package org.vectrics.common.view;

import org.apache.ecs.html.Div;
import org.apache.ecs.html.Input;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Combo;
import org.vectrics.common.html.Row;


public abstract class HtmlFormRow  {
	private Row row = null;
	private Cell labelCell = null;
	private Cell valueCell = null;
	private Div valueDiv = null;
	private Div labelDiv = null;
	private HtmlForm form = null;
	private Logger log = Logger.getLogger(this.getClass());
	private String paramName = null;
	
	public HtmlFormRow(HtmlForm form, String paramName, boolean required) {
		super();
		this.form = form;
		if (form == null)
			throw new SystemException("Passed HtmlForm is null");
		this.row = form.addRow();
		this.paramName = paramName;
		createCells(paramName);
	}
	
	public HtmlForm getHtmlForm() {
		return this.form;
	}

	public String getParamName() {
		return(this.paramName);
	}
	
	public Div getValueDiv() {
		return(valueDiv);
	}

	public Div getLabelDiv() {
		return(labelDiv);
	}
	
	private void createCells(String paramName) {
		labelCell = row.addCell();
    	labelDiv = new Div();
    	labelDiv.setID("divlabel_" + paramName);
    	labelDiv.setClass("formLabelDiv");
    	labelDiv.setStyle("position:relative;left:3px");
        labelCell.addElement(labelDiv);
		
		valueCell = row.addCell();
    	valueDiv = new Div();
    	valueDiv.setID("div_" + paramName);
        valueCell.addElement(valueDiv);
	}

	
    public Input makeEdit(String name) {
    	String id = "edit_" + name;
    	Input titleEdit = new Input();
        titleEdit.setType(Input.text);
        titleEdit.setName(name);
        titleEdit.setClass("formEditClass");
        titleEdit.setID(id);
        String value = this.form.getParameterValue(name);
        log.info("Edit box, param = " + name + "  Value = " + value);
        titleEdit.setValue(value);
        
    	this.form.setFocusElementIfNull(id);
   
        return(titleEdit);
    }
    
    public Combo makeCombo(String name) {
    	String id = "combo_" + name;
    	Combo combo = new Combo();
    	combo.setName(name);
        combo.setID(id);
        String value = this.form.getParameterValue(name);
        log.info("Edit box, param = " + name + "  Value = " + value);
        
    	this.form.setFocusElementIfNull(id);
   
        return(combo);
    }
}
